package test.closure
//普通参数：
//
//一个闭包的普通参数定义必须遵循如下一些原则：
//
//参数类型可选
//参数名字
//可选的参数默认值
//参数必须用逗号分隔
def closureWithOneArg = { str -> str.toUpperCase() }
assert closureWithOneArg('groovy') == 'GROOVY'

def closureWithOneArgAndExplicitType = { String str -> str.toUpperCase() }
assert closureWithOneArgAndExplicitType('groovy') == 'GROOVY'

def closureWithTwoArgs = { a, b -> a + b }
assert closureWithTwoArgs(1, 2) == 3

def closureWithTwoArgsAndExplicitTypes = { int a, int b -> a + b }
assert closureWithTwoArgsAndExplicitTypes(1, 2) == 3

def closureWithTwoArgsAndOptionalTypes = { a, int b -> a + b }
assert closureWithTwoArgsAndOptionalTypes(1, 2) == 3

def closureWithTwoArgAndDefaultValue = { int a, int b = 2 -> a + b }
assert closureWithTwoArgAndDefaultValue(1) == 3

//隐含参数：
//
//当一个闭包没有显式定义一个参数列表时，闭包总是有一个隐式的it参数。如下：
def greeting = { "Hello, $it!" }
assert greeting('Patrick') == 'Hello, Patrick!'

//可变长参数：
def concat1 = { String... args -> args.join('') }
assert concat1('abc', 'def') == 'abcdef'
def concat2 = { String[] args -> args.join('') }
assert concat2('abc', 'def') == 'abcdef'

def multiConcat = { int n, String... args ->
    args.join('') * n
}
assert multiConcat(2, 'abc', 'def') == 'abcdefabcdef'

//很多方法的最后一个参数都是一个闭包，我们可以在这样的方法调运时进行略写括弧。比如：

def debugClosure(int num, String str, Closure closure) {
    //dosomething
}

debugClosure(1, "groovy", {
    println "hello groovy!"
})